import json

import matplotlib.pyplot as plt


def analyze_and_plot_categories(input_file, output_file):
    category_count = {}
    category_score_sum = {}

    with open(input_file, 'r') as infile:
        for line in infile:
            try:
                log_entry = json.loads(line)
                category = log_entry.get('category', 'unknown')
                score = log_entry.get('score', 0.0)

                if category in category_count:
                    category_count[category] += 1
                    category_score_sum[category] += score
                else:
                    category_count[category] = 1
                    category_score_sum[category] = score
            except json.JSONDecodeError:
                print(f"Error decoding JSON from line: {line}")

    # Calculate total count
    total_count = sum(category_count.values())

    # Calculate average score for each category
    category_average_score = {category: category_score_sum[category] / category_count[category] for category in
                              category_count}

    # Write analysis results to output file
    with open(output_file, 'w') as outfile:
        outfile.write(f"Total number of entries: {total_count}\n")
        for category in category_count:
            outfile.write(
                f"Category: {category}, Count: {category_count[category]}, Average Score: {category_average_score[category]:.4f}\n")

    # Plotting the pie chart
    labels = category_count.keys()
    sizes = category_count.values()
    colors = plt.cm.tab20.colors[:len(labels)]
    explode = [0.1 if i == max(sizes) else 0 for i in sizes]  # Explode the largest slice

    plt.figure(figsize=(10, 8))
    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)
    plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    plt.title('Distribution of Sensitive Word Categories')
    plt.show()


# 使用函数
analyze_and_plot_categories('filter_result.json', 'data_analyze.txt')
